<?php

namespace App\Http\Repository;

use App\Fragment;
use App\Info;

class FragmentRepository
{
    private $fragment;
    private $info;

    public function __construct(Fragment $fragment, Info $info)
    {
        $this->fragment = $fragment;
        $this->info = $info;
    }

    //得到碎片分组
    public function getFragmentGroupChunk()
    {
        $tmpFragments = $this->fragment
            ->rightJoin('chunks', 'chunks.id', '=', 'fragments.chunk')
            ->select('chunks.id as chunk_id', 'chunks.name as chunk_name', 'fragments.*')
            ->get();

        foreach ($tmpFragments as $fragment) {
            $group[$fragment->chunk_id]['name'] = $fragment->chunk_name;
            if (!empty($fragment->id)) {
                $group[$fragment->chunk_id]['data'][] = $fragment;
            }
        }

        return $group;
    }

    //得到携带数据的碎片标签
    public function getInfoGroupFragment()
    {
        $fragments = $this->fragment
            ->join('fragment_sort_lines', 'fragment_sort_lines.fragment', '=', 'fragments.id')
            ->where('fragments.status', 1)
            ->select('fragments.*', \DB::raw('group_concat(fragment_sort_lines.sort) as sort'))
            ->groupBy('fragments.id')
            ->get();
        $frag = [];
        foreach ($fragments as $fragment) {
            $desc = $fragment->type == 1 ? 'xindex' : 'page_view';
            $frag[$fragment->path] = $this->info
                ->join('info_sort_lines', 'infos.id', '=', 'info_sort_lines.info')
                ->join('sorts', 'sorts.id', '=', 'info_sort_lines.sort')
                ->join('chunks', 'chunks.id', '=', 'info_sort_lines.chunk')
                ->leftjoin('info_image_lines', 'info_image_lines.info', '=', 'infos.id')
                ->leftjoin('images', 'images.id', '=', 'info_image_lines.image')
                ->where('infos.status', 1)
                ->whereNull('set_time_out')
                ->where('chunks.status', 1)
                ->where('sorts.status', 1)
                ->where('info_sort_lines.chunk', $fragment->chunk)
                ->whereIn('info_sort_lines.sort', explode(',', $fragment->sort))
                ->where(function ($query) use ($fragment) {
                    if ((bool)$fragment->is_hot) {
                        $query->where('infos.is_hot', 1);
                    }
                })
                ->where(function ($query) use ($fragment) {
                    if ((bool)$fragment->is_push) {
                        $query->where('infos.is_push', 1);
                    }
                })
                ->where(function ($query) use ($fragment) {
                    if ((bool)$fragment->is_new) {
                        $query->where('infos.is_new', 1);
                    }
                })
                ->take($fragment->num)
                ->groupBy('infos.id')
                ->orderBy('infos.' . $desc, 'desc')
                ->orderBy('infos.created_at', 'desc')
                ->select('infos.*', 'infos.created_at as time', 'images.path as image', 'images.water', 'sorts.id as sort')
                ->get()
                ->toArray();
            foreach ($frag[$fragment->path] as $k => $info) {
                $frag[$fragment->path][$k]['image'] = !empty($info['water']) ? $info['water'] : $info['image'];
            }
        }

        return $frag;
    }
}